package com.example.entity;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Util {
    public static Object[][] convertTo2DArrayWithoutHeader(List<Map<String, Object>> data) {
        if (data == null) {
            return new Object[0][0];
        }

        Set<String> allKeys = new LinkedHashSet<>();
        for (Map<String, Object> row : data) {
            if (row != null) {
                allKeys.addAll(row.keySet());
            }
        }

        String[] columns = allKeys.toArray(new String[0]);
        Object[][] result = new Object[data.size()][columns.length];

        for (int i = 0; i < data.size(); i++) {
            Map<String, Object> row = data.get(i);
            if (row == null) {
                continue;
            }
            for (int j = 0; j < columns.length; j++) {
                result[i][j] = row.getOrDefault(columns[j], null);
            }
        }

        return result;
    }

}
